'use strict';

angular.module('app')
	.controller('dmAuthManagerController', [ '$scope', '$rootScope', '$state', '$timeout', 'toaster',
	                                function($scope, $rootScope, $state, $timeout, toaster) {
		$scope.title = '授权管理';
        $scope.param = { };
        $scope.loading = false;
        
		$scope.search = function () {
	        $scope.loading = true;
	        if($scope.pageFlag != 1){//未点击翻页
			  	   $scope.param.pageNum=1;//将页码设置为1，即跳转回第一页
			  	}else{//点击翻页
			  	   $scope.pageFlag = 2;//将开关值设置为2
			  	}
			$.ajax({
				type: 'PUT',
	            dataType: 'json',
				contentType:'application/json;charset=UTF-8',
				url : '/doorSysManager/authManager/list',
				data: angular.toJson($scope.param)
			}).then(function(result) {
		        $scope.loading = false;
				if (result.httpCode == 200) {
					$scope.pageInfo = result;
				} else {
					$scope.msg = result.msg;
				}
				$scope.$apply();
			});
		}
		
		$scope.search();
		
		$scope.clearSearch = function() {
			$scope.param.keyword= null;
			$scope.search();
		}
		
		$scope.disableItem = function(id, enable) {
			
		}
		
		$scope.delAuth = function(cardNo){

			 $scope.delParam = { };
			 $scope.delParam.cardNo = cardNo;
			 $.ajax({
	 				type: 'POST',
	 	            dataType: 'json',
	 				contentType:'application/json;charset=UTF-8',
	 				url : '/doorSysManager/authManager/delAuth',
	 				data: angular.toJson($scope.delParam)
	 			}).then(function(result){
	 					if(result.httpCode ==200){//成功
	                    toaster.clear('*');
	                    toaster.pop('success', '', "保存成功");
	                    $timeout(function(){
	                    	$state.go('main.doorSysManager.authManager.list',null,{
	                            reload:true
	                        });
	                    },1000);
	                }else{
	                    toaster.clear('*');
	                    toaster.pop('error', '', result.msg);
	                    $scope.isDisabled = false;
	                }
	 			});
			
		}
		
		// 翻页
        $scope.pagination = function (page) {
            $scope.param.pageNum=page;
            $scope.pageFlag = 1;//将开关值设置为1
            $scope.search();
        };
} ]);